package com.woniu.shop.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.Result;
import com.woniu.shop.pojo.Shop;
import com.woniu.shop.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaohuang
 * @since 2022-11-16
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "门店管理")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @ApiOperation("添加")
    @PostMapping
    public String saveShop(@RequestBody Shop shop){
        boolean save = shopService.save(shop);
        return save == true? "添加成功":"添加失败";
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<String> updateShop(@RequestBody Shop shop){
        boolean b = shopService.updateById(shop);
        return b ? Result.ok():Result.fail();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public Result<Shop> getById(@PathVariable Integer id){
        Shop shop = shopService.getById(id);
        return Result.ok(shop);
    }

    @ApiOperation("通过userId查询")
    @GetMapping("/user/{userId}")
    public Result<Integer> getShopId(@PathVariable Integer userId){
        Shop shop = shopService.getOne(Wrappers.lambdaQuery(Shop.class).eq(Shop::getUserId, userId));
        return Result.ok(shop.getId());
    }
}
